package JDBC;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;

public class JDBCDemo {
    @Test
    public void test1(){
        try {
            // 1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获得连接对象connection
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java1?useUnicode=true&characterEncoding=UTF-8", "root", "root");
            // 3.写SQL语句
            String sql = "SELECT id,`name`,age,gender FROM student";
            // 4、创建Statement(一艘船)
            Statement statement = connection.createStatement();
            // 5、执行sql语句
            // (1) 更新类（更改了表里面数据）：delete/update/insert executeUpdate()
            // 返回值：int，表示你影响的行数
            // (2)查询（没有改变表里面数据）: select executeQuery()
            // 返回值：结果集ResultSet
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()){
                // 每遍历一次循环，就拿出数据库查询结果里面的一行记录，就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id,name,age,gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test2(){
        int selectId = 2;
        Connection connection = null;
        PreparedStatement statement =null;
        ResultSet resultSet = null;
        ArrayList<Student> list = new ArrayList<Student>();
        try {
            // 1.加载驱动
            // 2.获得连接对象connection
            connection = JDBCUtil.getConnnection();
            // 3.写SQL语句
            String sql = "SELECT id,`name`,age,gender FROM student where id > ?";//占位符
            // 4、PreparedStatement使用预编译的SQL语句,
            statement = connection.prepareStatement(sql);
//            传参
            statement.setInt(1,selectId);
//            打印SQL语句
            System.out.println(statement);
            // 5、执行sql语句
            // (1) 更新类（更改了表里面数据）：delete/update/insert executeUpdate()
            // 返回值：int，表示你影响的行数
            // (2)查询（没有改变表里面数据）: select executeQuery()
            // 返回值：结果集ResultSet
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                // 每遍历一次循环，就拿出数据库查询结果里面的一行记录，就是一个学生的信息,
                // 就可以封装成一个Student对象
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id,name,age,gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {//关闭连接
            JDBCUtil.close(connection,statement,resultSet);
        }
    }
    @Test //SQL删除
    public void testDelete(){
        int selectId = 2;
        Connection connection = null;
        PreparedStatement statement =null;
        try {
            connection = JDBCUtil.getConnnection();
            String sql = "delete FROM student where id = ?";//占位符
            statement = connection.prepareStatement(sql);
            statement.setInt(1,selectId);
            System.out.println(statement);
            int cntRow = statement.executeUpdate();
            System.out.println("影响的行数"+cntRow);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {//关闭连接
            JDBCUtil.close(connection,statement,null);
        }
    }
    @Test //SQL插入记录
    public void testInsert(){
        String name = "陈振国";
        Integer age = 23;
        String gender = "男";
        Connection connection = null;
        PreparedStatement statement =null;
        try {
            connection = JDBCUtil.getConnnection();
            String sql = "insert into student(`name` ,`age`,`gender`) values (?,?,?)";//占位符
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,age);
            statement.setString(3,gender);
            System.out.println(statement);
            int cntRow = statement.executeUpdate();
            System.out.println("影响的行数"+cntRow);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {//关闭连接
            JDBCUtil.close(connection,statement,null);
        }
    }
    @Test //SQL修改记录
    public void testUpdate(){
        String name = "akpower";
        Connection connection = null;
        PreparedStatement statement =null;
        try {
            connection = JDBCUtil.getConnnection();
            String sql = "update student set `name` = ? where `id` = ?";//占位符
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,4);
            System.out.println(statement);
            int cntRow = statement.executeUpdate();
            System.out.println("影响的行数"+cntRow);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {//关闭连接
            JDBCUtil.close(connection,statement,null);
        }
    }
}
